package com.hugui.stock.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hugui.stock.entity.StockDict;
import com.hugui.stock.entity.StockMarket;
import com.hugui.stock.service.IStockDictService;
import com.hugui.stock.service.IStockKlineService;
import com.hugui.stock.service.IStockMarketService;
import com.hugui.stock.service.StockApiService;
import com.hugui.stock.service.ConcurrentStockDataService;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hugui123
 * @since 2019-07-10
 */
@RestController
@RequestMapping("/stockDict")
public class StockDictController {

	private Logger logger = LoggerFactory.getLogger(StockDictController.class);

	@Autowired
	private IStockDictService stockDictService;

	@Autowired
	private IStockMarketService stockMarketService;

	@Autowired
	private IStockKlineService stockKlineService;
	
	@Autowired
	private StockApiService stockApiService;
	
	@Autowired
	private ConcurrentStockDataService concurrentStockDataService;

	@PostConstruct
	public void init() {

		int count = stockDictService.selectCount(null);
		if (count > 0) {
			return;
		}

		try (FileInputStream fis = new FileInputStream(new File("./data/stockcode.txt"))) {

			InputStreamReader reader = new InputStreamReader(fis, "UTF-8");
			BufferedReader br = new BufferedReader(reader);
			String line;
			while ((line = br.readLine()) != null) {
				String[] stockInfos = line.split("\\)");
				if (stockInfos.length > 0) {
					List<StockDict> stockList = new ArrayList<>();
					for (String stockInfo : stockInfos) {

						String[] stock = stockInfo.split("\\(");

						String code = stock[1];
						String base = null;
						if (code.startsWith("6")) {
							base = "sh";
						} else {
							base = "sz";
						}
						stockList.add(StockDict.builder().code(stock[1]).name(stock[0]).base(base).build());

					}

					if (!stockList.isEmpty()) {
						stockDictService.insertBatch(stockList);
					}
				}
			}
		} catch (IOException e) {
		}
	}

	/**
	 * 并发收集股票数据
	 */
	@GetMapping("/collectConcurrent/{stockCode}")
	public String collectStockDataConcurrently(@PathVariable("stockCode") String stockCode) {
		logger.info("开始并发收集股票数据，股票代码: {}", stockCode);
		
		try {
			concurrentStockDataService.collectStockDataConcurrently(stockCode);
			return "并发数据收集完成";
		} catch (Exception e) {
			logger.error("并发数据收集失败", e);
			return "并发数据收集失败: " + e.getMessage();
		}
	}

	/**
	 * 将本地文件数据导入数据库
	 */
	@GetMapping("/loadintodb")
	public String loadIntoDb() {
		logger.info("开始并发收集股票数据");

		try {
			// 分析数据并保存到数据库
			concurrentStockDataService.analyzeAndSaveToDatabase();
			return "分析数据并保存到数据库完成";

		} catch (Exception e) {
			logger.error("分析数据并保存到数据库失败", e);
			return "分析数据并保存到数据库失败: " + e.getMessage();
		}
	}
}